package cn.iocoder.yudao.module.member.controller.admin.userpackagedetail;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.userpackagedetail.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userpackagedetail.UserPackageDetailDO;
import cn.iocoder.yudao.module.member.service.userpackagedetail.UserPackageDetailService;

@Tag(name = "管理后台 - 用户套餐购买明细")
@RestController
@RequestMapping("/member/user-package-detail")
@Validated
public class UserPackageDetailController {

    @Resource
    private UserPackageDetailService userPackageDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建用户套餐购买明细")
    @PreAuthorize("@ss.hasPermission('member:user-package-detail:create')")
    public CommonResult<Long> createUserPackageDetail(@Valid @RequestBody UserPackageDetailSaveReqVO createReqVO) {
        return success(userPackageDetailService.createUserPackageDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户套餐购买明细")
    @PreAuthorize("@ss.hasPermission('member:user-package-detail:update')")
    public CommonResult<Boolean> updateUserPackageDetail(@Valid @RequestBody UserPackageDetailSaveReqVO updateReqVO) {
        userPackageDetailService.updateUserPackageDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户套餐购买明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-package-detail:delete')")
    public CommonResult<Boolean> deleteUserPackageDetail(@RequestParam("id") Long id) {
        userPackageDetailService.deleteUserPackageDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户套餐购买明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-package-detail:query')")
    public CommonResult<UserPackageDetailRespVO> getUserPackageDetail(@RequestParam("id") Long id) {
        UserPackageDetailDO userPackageDetail = userPackageDetailService.getUserPackageDetail(id);
        return success(BeanUtils.toBean(userPackageDetail, UserPackageDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户套餐购买明细分页")
    @PreAuthorize("@ss.hasPermission('member:user-package-detail:query')")
    public CommonResult<PageResult<UserPackageDetailRespVO>> getUserPackageDetailPage(@Valid UserPackageDetailPageReqVO pageReqVO) {
        PageResult<UserPackageDetailDO> pageResult = userPackageDetailService.getUserPackageDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, UserPackageDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户套餐购买明细 Excel")
    @PreAuthorize("@ss.hasPermission('member:user-package-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserPackageDetailExcel(@Valid UserPackageDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserPackageDetailDO> list = userPackageDetailService.getUserPackageDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户套餐购买明细.xls", "数据", UserPackageDetailRespVO.class,
                        BeanUtils.toBean(list, UserPackageDetailRespVO.class));
    }

}